package com.gz.controller;

import com.gz.dto.GradeDto;
import com.gz.entity.Grade;
import com.gz.service.GradeService;
import com.gz.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * (Grade)表控制层
 *
 * @author makejava
 * @since 2024-01-29 23:54:31
 */
@RestController
@RequestMapping("grade")
@Api(value = "年级控制层", tags = {"年级控制层"})
public class GradeController {

  /**
   * 服务对象
   */
  @Autowired
  private GradeService gradeService;

  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @ApiOperation(value = "分页查询")
  @PostMapping("/queryPage")
  public R queryByPage(@RequestBody GradeDto dto, HttpServletRequest request) {
    return R.ok(gradeService.queryByPage(dto));
  }


  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @ApiOperation(value = "条件查询")
  @PostMapping("/queryAll")
  public R queryAll(@RequestBody GradeDto dto, HttpServletRequest request) {
    if(null!=dto.getCrtId()&&null!=dto.getRId()&&1==dto.getRId()){
      dto.setCrtId(null);
    }
    return R.ok(gradeService.queryAll(dto));
  }
  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @ApiOperation(value = "条件查询看是否有编辑权限")
  @PostMapping("/queryAllForIfCanEdit")
  public R queryAllForIfCanEdit(@RequestBody GradeDto dto, HttpServletRequest request) {
    return R.ok(gradeService.queryAllForIfCanEdit(dto));
  }

  /**
   * 通过主键查询单条数据
   *
   * @param id 主键
   * @return 单条数据
   */
  @ApiOperation(value = "主键查询")
  @GetMapping("/queryById")
  public R<Grade> queryById(@RequestParam("id") Integer id) {
    return R.ok(gradeService.queryById(id));
  }

  /**
   * 新增数据
   *
   * @param grade 实体
   * @return 新增结果
   */
  @ApiOperation(value = "新增数据")
  @PostMapping("/save")
  public R<Grade> add(@RequestBody Grade grade) {
    GradeDto dto = new GradeDto();
    dto.setValue(grade.getValue());
    List<Grade> grades = gradeService.queryAll(dto);
    if(CollectionUtils.isNotEmpty(grades)){
      return R.failure("该年级已经存在，请重新确认");
    }
    return R.ok(gradeService.insert(grade));
  }

  /**
   * 编辑数据
   *
   * @param grade 实体
   * @return 编辑结果
   */
  @ApiOperation(value = "编辑数据")
  @PostMapping("/update")
  public R<Grade> edit(@RequestBody Grade grade) {
    return R.ok(gradeService.update(grade));
  }

  /**
   * 删除数据
   *
   * @param id 主键
   * @return 删除是否成功
   */
  @GetMapping("/deleteById")
  @ApiOperation(value = "删除数据")
  public R<Boolean> deleteById(@RequestParam("id") Integer id) {
    return R.ok(gradeService.deleteById(id));
  }

}

